package com.yoomiito.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.yoomiito.app.ImmersionBarFragment;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.TabBean;
import com.yoomiito.app.ui.fragment.cicle.CicleFragment;
import com.yoomiito.app.ui.fragment.cicle.CircleGoodRecommendFragment;
import com.yoomiito.app.ui.fragment.cicle.NewMaterialFragment;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.ResUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 发现   487852
 */
public class FindFragment extends ImmersionBarFragment {
//    @BindView(R.id.tv_title)
//    TitleView titleView;
    @BindView(R.id.tv_center)
    TextView titleView;
    @BindView(R.id.iv_back_left)
    ImageView iv;
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;

    private List<TabBean> mTabList = new ArrayList<>();
    private List<Fragment> mFragmentList = new ArrayList<>();
    private CicleFragment mCicleFragment;
    private PopupWindow popupWindow;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_find;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        titleView.setText("素材圈");
        titleView.getPaint().setFakeBoldText(true);
        iv.setVisibility(View.INVISIBLE);
//        titleView.setTitleStyle();
//        L.i("圈子----公司id：" + UserInfoConfig.getCompanyId());
//        if (UserInfoConfig.getCompanyId() != 1) {
//            titleView.setCenterText("瑰秘圈");
//        }
//        ImmersionBar.setTitleBar(context, titleView);
//        titleView.setOnRightClick(view -> {//Promotional material
////                showPopupWindow(titleView);
//            if (!App.mAppConfig.getIsLogin()) {
//                AllUtils.toLogin(context, AllCode.LoginIn);
//                return;
//            }
//            startActivityForResult(new Intent(context, CiclePublishActivity.class), 0);
//        });
//        mTabList.add(new TabBean(ResUtil.getString(R.string.my_publish_material), 30, false));
        mTabList.add(new TabBean("宣传素材", 30, false));
        mTabList.add(new TabBean(ResUtil.getString(R.string.cicle_remommend_goods), 30, false));
//        mTabList.add(new TabBean(ResUtil.getString(R.string.cicle_communication), 5, false));
//        mTabList.add(new TabBean(ResUtil.getString(R.string.cicle_live), 5, false));

//        mCicleFragment = new CicleFragment();
//        mFragmentList.add(new PromotionalMaterialFragment());
        mFragmentList.add(new NewMaterialFragment());
        mFragmentList.add(new CircleGoodRecommendFragment());
//        mFragmentList.add(mCicleFragment);
//        mFragmentList.add(new LiveFragment());

        initViewPager();
    }

    private void initViewPager() {
        // 提供自定义的布局添加Tab
        if (mTabList != null) {
            for (int i = 0; i < mTabList.size(); i++) {
                View view = LayoutInflater.from(context).inflate(R.layout.tab_item_cicle, null);
                TextView tabText = (TextView) view.findViewById(R.id.tab_content);
                TextView tabNumText = (TextView) view.findViewById(R.id.tab_num);
                TabBean tabBean = mTabList.get(i);
                tabText.setText(tabBean.content);
                if (tabBean.isShowMoney) {
                    tabNumText.setText("(" + tabBean.num + ")");
                    tabNumText.setVisibility(View.VISIBLE);
                } else {
                    tabNumText.setVisibility(View.GONE);
                }
                mTabLayout.addTab(mTabLayout.newTab().setCustomView(view));
            }
        }
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                onTabItemSelected(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        onTabItemSelected(0);
    }

    private void onTabItemSelected(int index) {
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment;
        for (int i = 0, j = mFragmentList.size(); i < j; i++) {
            fragment = mFragmentList.get(i);
            if (fragment.isAdded()) {
                fragmentTransaction.hide(fragment);
            }
        }
        fragment = mFragmentList.get(index);
        if (fragment.isAdded()) {
            fragmentTransaction.show(fragment);
        } else {
            fragmentTransaction.add(R.id.fragment_container, fragment);
        }
        fragmentTransaction.commitAllowingStateLoss();


        // Tab 选中之后，改变各个Tab的状态
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            View view = mTabLayout.getTabAt(i).getCustomView();
            View tabView = view.findViewById(R.id.tab_view);
            TextView contentTv = view.findViewById(R.id.tab_content);
            if (i == index) { // 选中状态
                tabView.setVisibility(View.VISIBLE);
                contentTv.setTextColor(ResUtil.getColor(R.color.color_balk_333333));
            } else {// 未选中状态
                tabView.setVisibility(View.INVISIBLE);
                contentTv.setTextColor(ResUtil.getColor(R.color.color_balk_999999));
            }
        }
    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarDarkFont(true)   //状态栏字体是深色，不写默认为亮色
//                .navigationBarDarkIcon(true) //导航栏图标是深色，不写默认为亮色
                .fitsSystemWindows(true)
//                .statusBarColor(R.color.colorPrimary)
//                .statusBarDarkFont(true)
//                .navigationBarColor(R.color.colorPrimary)
                .init();
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
//        L.i("圈子-----收到消息");
//        if (AllCode.LoginOut.equals(eventMessage.getEventType())) {
//            L.i("圈子-----收到退出登录消息");
//            if (titleView != null)
//                titleView.setCenterText("优米圈");
//        }
//        if (AllCode.LoginSuccess.equals(eventMessage.getEventType())) {
//            L.i("圈子-----收到登录成功消息");
//            if (titleView == null) return;
//            if (UserInfoConfig.getCompanyId() == 1) {
//                titleView.setCenterText("优米圈");
//            } else {
//                titleView.setCenterText("瑰秘圈");
//            }
//        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            //发布圈子通知刷新
//        if (resultCode == CiclePublishActivity.CODE_PUBLISH_CICLE) {
//            onTabItemSelected(1);
//            if (mCicleFragment != null) {
//                mCicleFragment.onActivityResult(requestCode, resultCode, data);
//            }
//        }
    }

}
